<template>
  <div class="page-header">
    <!-- 左侧按钮 -->
    <div class="header-left">
      <button 
        v-if="showLeftButton" 
        class="header-button left-button"
        :class="{ primary: leftButtonPrimary }"
        @click="handleLeftButtonClick"
      >
        <span v-if="leftIcon" class="header-icon">{{ leftIcon }}</span>
        <span v-if="leftText">{{ leftText }}</span>
      </button>
      <div v-else class="header-button-placeholder"></div>
    </div>
    
    <!-- 标题 -->
    <h1 class="header-title">{{ title }}</h1>
    
    <!-- 右侧按钮或自定义内容 -->
    <div class="header-right">
      <slot name="right">
        <button 
          v-if="showRightButton" 
          class="header-button right-button"
          :class="{ primary: rightButtonPrimary }"
          @click="handleRightButtonClick"
        >
          <span v-if="rightIcon" class="header-icon">{{ rightIcon }}</span>
          <span v-if="rightText">{{ rightText }}</span>
        </button>
        <div v-else class="header-button-placeholder"></div>
      </slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PageHeader',
  props: {
    // 标题
    title: {
      type: String,
      required: true
    },
    // 左侧按钮配置
    showLeftButton: {
      type: Boolean,
      default: true
    },
    leftText: {
      type: String,
      default: '返回'
    },
    leftIcon: {
      type: String,
      default: '←'
    },
    leftButtonPrimary: {
      type: Boolean,
      default: false
    },
    // 右侧按钮配置
    showRightButton: {
      type: Boolean,
      default: false
    },
    rightText: {
      type: String,
      default: ''
    },
    rightIcon: {
      type: String,
      default: ''
    },
    rightButtonPrimary: {
      type: Boolean,
      default: true
    }
  },
  // 定义插槽
  slots: {
    // 右侧自定义内容插槽
    right: {
      description: '右侧自定义内容插槽'
    }
  },
  methods: {
    handleLeftButtonClick() {
      // 默认行为是返回上一页
      this.$emit('left-button-click', () => {
        this.$router.back()
      })
    },
    handleRightButtonClick() {
      this.$emit('right-button-click')
    }
  }
}
</script>

<style scoped>
.page-header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  padding: 0 16px;
  background-color: #ffffff;
  border-bottom: 1px solid #eeeeee;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.header-left,
.header-right {
  flex: 1;
  display: flex;
  align-items: center;
}

.header-left {
  justify-content: flex-start;
}

.header-right {
  justify-content: flex-end;
}

.header-title {
  flex: 1;
  text-align: center;
  font-size: 18px;
  font-weight: 500;
  margin: 0;
  color: #333333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-button {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 12px;
  border: none;
  background: transparent;
  font-size: 16px;
  color: #666666;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.2s;
}

.header-button:active {
  background-color: rgba(0, 0, 0, 0.05);
}

.header-button.primary {
  color: #409eff;
}

.header-icon {
  margin-right: 4px;
  font-size: 18px;
}

.header-button-placeholder {
  width: 80px;
  height: 100%;
}
</style>